package com.amoyt.project.domain.res;

import java.io.Serializable;

/**
 * @Date: 2025/8/6
 * @Time: 10:20
 * @Author: TQ_QT
 * @Description: 统一返回结果
 */
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 状态码  1成功，0和其它数字为失败
     */
    private Integer code;
    /**
     * 错误信息
     */
    private String msg;
    /**
     * 数据
     */
    private T data;

    /**
     * 默认成功类型
     */
    public static <T> Result<T> success() {
        Result<T> result = new Result<T>();
        result.code = 1;
        result.msg = "操作成功";
        return result;
    }

    /**
     * 默认成功类型，data为数据
     */
    public static <T> Result<T> success(T object) {
        Result<T> result = new Result<T>();
        result.data = object;
        result.code = 1;
        return result;
    }
    /**
     * 成功，携带提示信息
     */
    public static <T> Result<T> success(String msg, T object) {
        Result<T> result = new Result<T>();
        result.msg = msg;
        result.data = object;
        result.code = 1;
        return result;
    }

    /**
     * 基础失败类型，msg为错误信息
     */
    public static <T> Result<T> fail(String msg) {
        Result result = new Result();
        result.msg = msg;
        result.code = 0;
        return result;
    }

    /**
     * 自定义错误类型，code为错误码，msg为错误信息
     */
    public static <T> Result<T> fail(Integer code, String msg) {
        Result result = new Result();
        result.msg = msg;
        result.code = code;
        return result;
    }

    /**
     * 系统级别错误类型，msg为错误信息
     */
    public static <T> Result<T> error(String msg) {
        Result result = new Result();
        result.code = -1; // 系统级别错误
        result.msg = msg;
        return result;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
